import APIConfig from "../../config/api";
import cache from "../../enum/cache";
import Opinion from "../../model/opinion";
import FileUploader from "../../utils/file-uploader";
import {
  getDataSet,
  getEventParam
} from "../../utils/util";

Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    status: String,
    opinionId: String
  },
  data: {
    text: '',
    messageList: [],
    scrollHeight: 0,
    intoView: 0,
    userId: 0,
    fileIds: [], // 上传附件
  },
  observers: {
    async show(show) {
      if (show) {
        this.setData({
          userId: wx.getStorageSync(cache.COMMON_INFO).userId
        })
        await this._getDiscussOpinionList();
      }
    }
  },
  methods: {
    // 关闭讨论
    onClose() {
      this.triggerEvent('close-talk')
    },
    // 文件上传
    async handleSendFile() {
      const res = await wx.chooseMessageFile({
        count: 1,
        type: 'file'
      })
      let filePath = res.tempFiles[0].path
      let query = {
        fileRealName: res.tempFiles[0].name, // 文件名
        bucketName: 'taolun', // bucket名称
      }
      let result = await FileUploader.uploadDiscussFile(filePath, query)
      if (result.code === 200) {
        this.data.fileIds.push(result.data.id)
        this.handleSend()
      }
    },
    handleInput: function (event) {
      this.data.text = getEventParam(event, 'value')
    },
    // 获取列表
    async _getDiscussOpinionList() {
      let query = {
        opinionId: this.data.opinionId,
        discussType: this.data.status,
      }
      let {
        data
      } = await Opinion.getDiscussOpinionList(query)
      this.setData({
        messageList: data,
        intoView: data.length - 1
      })
    },
    // 发送
    async handleSend() {
      const text = this.data.text.trim();
      if (text === '' && this.data.fileIds.length === 0) {
        return
      }
      let query = {
        opinionId: this.data.opinionId,
        content: text,
        discussType: this.data.status,
        fileIds: this.data.fileIds
      }
      let res = await Opinion.addDiscussOpinion(query)
      if (res.code == 200) {
        this._getDiscussOpinionList()
      }
      this.setData({
        text: '',
        fileIds: []
      })
    },
    // 阅读pdf
    handleOpenFile(e) {
      let file = getDataSet(e, 'item');
      const token = wx.getStorageSync(cache.TOKEN)
      wx.downloadFile({
        url: `${APIConfig.baseUrl}/minioApp/getMinioUrlOut?fileName=${file.previewName}&bucketName=taolun`,
        header: {
          'Authorization': `Bearer ${token}`
        },
        success: (res) => {
          const filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            fileType: 'pdf',
            success: (res) => {
              console.log(res);
            }
          })
        },
        fail: (err) => {
          console.log(err);
        }
      })
    }
  }
})